package com.xiaocikuai.smallchunks.service.User.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaocikuai.smallchunks.entity.User.Duser;
import com.xiaocikuai.smallchunks.mapper.User.DuserMapper;
import com.xiaocikuai.smallchunks.service.User.IDuserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaocikuai.smallchunks.utils.JWTservice.JwtService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 * @author author1
 * @since 2024-07-11
 */
@Service
public class DuserServiceImpl extends ServiceImpl<DuserMapper, Duser> implements IDuserService {

    @Resource
    DuserMapper duserMapper;

    public QueryWrapper<Duser> buildWrapperBasedOnDuser(Duser duser) throws Exception{
         QueryWrapper<Duser> wrapper = new QueryWrapper<>();
         wrapper.eq("ifdis", duser.getIfdis()); //返回数据库中字段ifdis为的条件
         return wrapper;
    }

    List<Duser> entityList = new ArrayList<>();
int id = 1;

    public boolean function(Duser entity) throws Exception{
                        /*新增*/
        save(entity);   duserMapper.insert(entity);    //新增一个
        saveBatch(entityList);                         //新增多个

                        /*删除*/
        removeById(entity); duserMapper.deleteById(entity); //删除一个
        removeBatchByIds(entityList);                       //删除多个

                        /*更新*/
        updateById(entity); updateBatchById(entityList); // 更新一个/多个
        lambdaUpdate()
                .eq(Duser::getUsername,666) //设置更新的条件
                .set(Duser::getDuid,666)        //设置更新的字段
                .update();                      //调用更新方法

                    /*增添 或 更新*/
        saveOrUpdate(entity);   saveOrUpdateBatch(entityList);

                        /*查询*/
        getById(entity);   getById(entity.getDuid());
        Duser entity_get = getOne(                      //调用get方法
                lambdaQuery()                           //使用条件构造器
                        .eq(Duser::getUsername,6)   //设置查询条件
                        .eq(Duser::getPassword, 6)  //可设置多个条件
                        .select(Duser::getDuid)         //设置查询字段
        );
        List<Duser> entityList_get = (                  //明确返回值
               lambdaQuery()                            //使用条件构造器
                       .eq(Duser::getDuid, 6)       //设置查询条件
                       .select()                        //设置查询字段为全部
        ).list();                                       //调用条件的查询方法


        entity_get.getBalance();
        List<String> ListCount = entityList_get.stream()
                .map(Duser::getUsername)
                .toList();


        return true;
    }

//    public boolean updateBalance(float balance, Duser duser){
//
//
//
//        duserMapper.update
//    }




}
